﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using EquipmentConfig.Class;

namespace EquipmentConfig
{
    public partial class TotalFlowSetting : Form
    {
        private MainForm mf;

        public TotalFlowSetting(MainForm mainForm)
        {
            InitializeComponent();
            mf = mainForm;
        }

        private void TotalFlowSetting_Load(object sender, EventArgs e)
        {
            btnRead_Click(null, null);
        }

        /// <summary>
        /// 读取
        /// </summary>
        private void btnRead_Click(object sender, EventArgs e)
        {
            try
            {
                mf.timer2.Enabled = false;
                mf.recBuffer.Clear();
                MainForm.isSuccess = false;
                mf.SendCmd(Protocol.GetWriteDeviceCmd(0x0E, new byte[1] { Convert.ToByte(cbbType.SelectedIndex + 1) }));
                Thread sub = new Thread(() =>
                {
                    if (!mf.CheckReturnCmd())
                    {
                        Messages.ShowAlertDialog("读取累计流量失败！", false);
                    }
                    else
                    {
                        this.Invoke((MethodInvoker)delegate
                        {
                            nud2.Value = mf.totalFlowValue;
                        });
                    }
                });
                sub.IsBackground = true;
                sub.Start();
                mf.timer2.Enabled = true;
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog("读取累计流量失败！", ex);
            }
        }

        /// <summary>
        /// 写入
        /// </summary>
        private void btnWrite_Click(object sender, EventArgs e)
        {
            try
            {
                mf.timer2.Enabled = false;
                mf.recBuffer.Clear();
                MainForm.isSuccess = false;
                List<byte> listData = new List<byte>();
                listData.Add(Convert.ToByte(cbbType.SelectedIndex + 1));
                listData.AddRange(BitConverter.GetBytes(Convert.ToUInt64(nud2.Value * 100000)).Reverse<byte>().ToArray());
                mf.SendCmd(Protocol.GetWriteDeviceCmd(0x0F, listData.ToArray()));
                Thread sub = new Thread(() =>
                {
                    if (!mf.CheckReturnCmd())
                    {
                        Messages.ShowAlertDialog("写入累计流量失败！", false);
                    }
                });
                sub.IsBackground = true;
                sub.Start();
                mf.timer2.Enabled = true;
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog("写入累计流量失败！", ex);
            }
        }

        private void TotalFlowSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            mf.timer2.Enabled = true;
            this.Dispose();
        }
    }
}
